package com.kotlin.myapplication.mvp.ui

import android.os.Build
import android.view.View
import com.kotlin.myapplication.R
import com.kotlin.myapplication.base.BaseActivity
import com.kotlin.myapplication.bean.*
import com.kotlin.myapplication.mvp.model.CalentarContract
import com.kotlin.myapplication.mvp.presenter.MainPresenter
import com.kotlin.myapplication.utils.Logger
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast
import java.util.*

class MainActivity : BaseActivity(), CalentarContract.view {
    override fun showNews(news: NewsBean<NewsResult<RepData>>) {
        Logger.log(news.toString())
    }

    override fun showWeather(weather: Weather<WeatherResult<realtime>>) {
        Logger.log(weather.toString())
    }


    override lateinit var presenter: CalentarContract.Presonter


    override fun initView() {
        setContentView(R.layout.activity_main)
        presenter = MainPresenter(this)
        Logger.log("1111111111111111")
        selectButton.setOnClickListener {
            titleTextView.visibility = View.GONE
            selectButton.visibility = View.GONE
            contentTextView.visibility = View.GONE
            datePicker.visibility = View.VISIBLE
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            datePicker.setOnDateChangedListener { view, year, month, day ->
                var date: String = "${year}-${month + 1}-${day}"
                Logger.log(date)
                presenter.getCalentarDayData(date)
            }
        } else {
            datePicker.setCalendarViewShown(false)
            Logger.log("" + Build.VERSION.SDK_INT)

            var calendar: Calendar = Calendar.getInstance()
            var year = calendar.get(Calendar.YEAR)
            var month = calendar.get(Calendar.MONTH)
            var date = calendar.get(Calendar.DATE)

            datePicker.init(year, month, date) { view, y, monthOfYear, dayOfMonth ->
                var date: String = "${y}-${monthOfYear + 1}-${dayOfMonth}"
                Logger.log(date)
                presenter.getCalentarDayData(date)
            }
            // new的部分哟那个{}代替
//            datePicker.init(year, month, date, new DatePicker.OnDateChangedListener() {
//                @Override
//                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                }
//            });

        }

        presenter.getWeather("南京")

        presenter.getNews("yule")

    }

    override fun onResume() {
        super.onResume()
        presenter.subscribe()
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.unsubscribe()
    }


    override fun showCalentarDay(calentarda: CalentarDayBean<CalentarDayResult<CalentarDayData>>) {
        datePicker.visibility = View.GONE
        titleTextView.visibility = View.VISIBLE
        selectButton.visibility = View.VISIBLE
        contentTextView.visibility = View.VISIBLE
        titleTextView.text = calentarda.result.data.date
        contentTextView.text = calentarda.result.data.toString()
    }

    override fun error(error: String) {
        toast(error)
    }


}
